/**
 * 
 */
package fr.ants.view.panel;

import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.TransferHandler;

import fr.ants.controller.command.ApplySimulationCommand;
import fr.ants.controller.listener.ApplyButtonListener;
import fr.ants.controller.listener.ButtonsListener;
import fr.ants.model.Node;
import fr.ants.view.HMI;
import fr.ants.view.Selection;
import fr.ants.view.component.SliderPanel;

/**
 * Panel that contain all components to configure the simulation.
 */
public class SimulationPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7907245630602707244L;
	/**
	 * Panel of the tools buttons
	 */
	private final ButtonsToolsPanel buttonsToolsPanel;
	/**
	 * apply the modification.
	 */
	private final JButton buttonApply;
	/**
	 * slier panel to configure the size of the brush.
	 */
	private final SliderPanel sizeSliderPanel;
	/**
	 * slier panel to configure the difficulty of a obstacle.
	 */
	private final SliderPanel difficultySliderPanel;
	/**
	 * slier panel to configure the speed of the renewal.
	 */
	private final SliderPanel speedSliderPanel;
	/**
	 * slier panel to configure the quantity of the renewal.
	 */
	private final SliderPanel capacityResourceSliderPanel;

	/**
	 * node selected.
	 */
	private Node node;

	/**
	 * Build.
	 * 
	 * @param applyButtonListener
	 *            listener for the button apply
	 * @param transferHandler
	 *            for the drag and drop
	 * @param buttonsListener
	 *            listener for the other buttons
	 * 
	 */
	public SimulationPanel(final ApplyButtonListener applyButtonListener,
			ButtonsListener buttonsListener, TransferHandler transferHandler) {
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		buttonsToolsPanel = new ButtonsToolsPanel(buttonsListener,
				transferHandler);
		add(buttonsToolsPanel);

		//slider to change the size of the brush
		sizeSliderPanel = new SliderPanel(HMI.BRUSH);
		sizeSliderPanel.setMaximum(10);
		sizeSliderPanel.setMinimum(1);
		add(sizeSliderPanel);

		//slider to change the difficulty
		difficultySliderPanel = new SliderPanel(HMI.DIFFICULTY);
		difficultySliderPanel.setMaximum(100);
		difficultySliderPanel.setMinimum(0);
		add(difficultySliderPanel);
		
		//slider to change the speed of the simulation
		speedSliderPanel = new SliderPanel(HMI.SPEED);
		speedSliderPanel.setMaximum(255);
		speedSliderPanel.setMinimum(1);
		add(speedSliderPanel);

		//slider to change the capacity of a resource
		capacityResourceSliderPanel = new SliderPanel(HMI.QUANTITY);
		capacityResourceSliderPanel.setMaximum(255);
		capacityResourceSliderPanel.setMinimum(1);
		add(capacityResourceSliderPanel);

		//button to apply the modification
		buttonApply = new JButton(HMI.APPLY);
		buttonApply.setEnabled(false);
		buttonApply.addActionListener(applyButtonListener);
		buttonApply.setActionCommand(HMI.APPLY_ACTION_SIMULATION);
		applyButtonListener
				.setApplySimulationCommand(new ApplySimulationCommand(this));
		add(buttonApply);
		add(Box.createRigidArea(new Dimension(0, 150)));
	}

	/**
	 * 
	 * @return size
	 */
	public int getSizeValue() {
		return sizeSliderPanel.getValue();
	}

	/**
	 * 
	 * @return difficulty
	 */
	public int getDifficultyValue() {
		return difficultySliderPanel.getValue();
	}

	/**
	 * 
	 * @return speed
	 */
	public int getSpeedValue() {
		return speedSliderPanel.getValue();
	}

	/**
	 * 
	 * @return capacity
	 */
	public int getCapacityValue() {
		return capacityResourceSliderPanel.getValue();
	}

	public Node getNode() {
		return node;
	}

	/**
	 * 
	 * @param node node to modify
	 */
	public void setNode(final Node node) {
		this.node = node;
	}

	/**
	 * Init the panel with a node.
	 */
	public void init(final Node node) {
		this.node = node;
		buttonApply.setEnabled(true);
		if (node.getResource() != null) {
			capacityResourceSliderPanel.setValue((int) node.getResource()
					.getCapacity());
			speedSliderPanel.setValue((int) node.getResource().getRegenRate());
		}
		difficultySliderPanel.setValue(node.getDifficulty());
	}

	/**
	 * to enable or not the button apply.
	 * 
	 * @param true to enable otherwise false
	 */
	public void setEnabledButtonApply(final boolean b) {
		buttonApply.setEnabled(b);

	}

	/**
	 * to enable or not the tools buttons.
	 * 
	 * @param true to enable otherwise false
	 */
	public void setEnabledButtonsTools(final boolean b) {
		buttonsToolsPanel.enableButtons(b);

	}
	
	/**
	 * to show what button is active or not
	 * 
	 * @param Selection
	 */
	public void setActiveButtonTools (Selection selection) {
		buttonsToolsPanel.activeButtons(selection);
	}
	
	/**
	 * to show what button is active or not
	 * 
	 * @param Selection
	 */
	public void setActiveSliders (Selection sel) {
		if(sel.equals(Selection.RESOURCE)) {
			sizeSliderPanel.setVisible(true);
			difficultySliderPanel.setVisible(false);
			capacityResourceSliderPanel.setVisible(true);
			speedSliderPanel.setVisible(true);
			buttonApply.setVisible(false);
		}
		
		else if(sel.equals(Selection.OBSTACLE)) {
			sizeSliderPanel.setVisible(true);
			difficultySliderPanel.setVisible(true);
			capacityResourceSliderPanel.setVisible(false);
			speedSliderPanel.setVisible(false);
			buttonApply.setVisible(false);
		}
		
		else if(sel.equals(Selection.SELECTION) || sel.equals(Selection.CLICK_RIGHT)) {
			sizeSliderPanel.setVisible(true);
			difficultySliderPanel.setVisible(true);
			capacityResourceSliderPanel.setVisible(true);
			speedSliderPanel.setVisible(true);
			buttonApply.setVisible(true);
		}
		else {
			sizeSliderPanel.setVisible(false);
			difficultySliderPanel.setVisible(false);
			capacityResourceSliderPanel.setVisible(false);
			speedSliderPanel.setVisible(false);
			buttonApply.setVisible(false);
		}
	}
	
}